﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Model.Calendar;
using Cn.Cellula.Biz.Def;

namespace Cn.Cellula.Biz.Impl {

    class CalendarBiz : MyBizSupport, ICalendarBiz {
        public void SaveOrUpdateMembers(String rTodoKey, String rMembers) { 
            if(String.IsNullOrEmpty(rMembers)){
                log.Info("Parm Error:members is null");
                return;
            }
            String hql = "from TCalendarTodoMembersEO eo where eo.TodoKey='"+rTodoKey+"' ";
            this.Delete(hql);
            String[] keys = rMembers.Split(',');
            for (int i = 0; i < keys.Length; i++) {
                TCalendarTodoMembersEO meo = new TCalendarTodoMembersEO();
                meo.MemberKey = keys[i];
                meo.TodoKey = rTodoKey;
                this.Save<TCalendarTodoMembersEO>(meo);
            }            
        }

        public Boolean IfOccurDtChecked(DateTime dt) {
            String hql = "from TCalendarMonthlyCheckEO eo where eo.CheckYear=" + dt.Year + " and eo.State=1 and eo.CheckMonth=" + dt.Month;
            IList <TCalendarMonthlyCheckEO> list = this.Find<TCalendarMonthlyCheckEO>(hql);
            return (list != null && list.Count > 0);
        }

        /// <summary>
        /// 构造检索Todo的HQL语句
        /// </summary>
        /// <param name="rOwnerKey">OwnerKey</param>
        /// <param name="rUserKey">操作者ID</param>
        /// <param name="rEdit">是否编辑TODO</param>
        /// <param name="rStartToPlan1">计划开始时间区间1</param>
        /// <param name="rStartToPlan2">计划开始时间区间2</param>
        /// <param name="rStatus">Todo的状态:开始，关闭，全部</param>
        /// <param name="rTodoKey">TodoKey</param>
        /// <param name="rOrderBy">排序字段，使用EO的属性名</param>
        /// <returns></returns>
        private String BuildHQL(String rOwnerKey, String rUserKey,Boolean rEdit, String rStartToPlan1, String rStartToPlan2, TodoStatus rStatus,String rTodoKey,String rOrderBy) {
            String hql = "from VCalendarTodoEO eo where eo.OwnerKey='" + rOwnerKey + "'";
            if (!String.IsNullOrEmpty(rStartToPlan1) && !String.IsNullOrEmpty(rStartToPlan2)) {
                hql += " and (eo.OccurDt>='" + rStartToPlan1 + "' and eo.OccurDt<='" + rStartToPlan2 + "' )";
            }
            if (!String.IsNullOrEmpty(rUserKey)) {
                if (rEdit) {
                    hql += " and ( eo.Operator='" + rUserKey + "' ";
                    hql += " or eo.AssignToUser='" + rUserKey + "') ";
                } else if(rUserKey != null){
                    if (rUserKey.Split(',').Length > 1) {
                        hql += " and ( ";
                        String[] keys = rUserKey.Split(',');
                        for (int i = 0; i < keys.Length; i++) {
                            if (i > 0) hql += " or ";
                            hql += " ( eo.Operator='" + keys[i] + "' ";
                            hql += " or eo.AssignToUser='" + keys[i] + "' ";
                            hql += " or eo.MembersKey like '%" + keys[i] + "%') ";
                        }
                        hql += " ) ";
                    } else {
                        hql += " and ( eo.Operator='" + rUserKey + "' ";
                        hql += " or eo.AssignToUser='" + rUserKey + "' ";
                        hql += " or eo.MembersKey like '%" + rUserKey + "%') ";
                    }
                }
            }
            if (TodoStatus.Started == rStatus) {
                hql += " and eo.IfClose=0 ";
            } else if (TodoStatus.Close == rStatus) {
                hql += " and eo.IfClose=1 ";
            }
            if (!String.IsNullOrEmpty(rTodoKey)) {
                hql += " and eo.TodoKey='"+rTodoKey+"' ";
            }
            if (!String.IsNullOrEmpty(rOrderBy)) {
                hql += " order by eo." + rOrderBy + " ";
            } else {
                hql += " order by eo.StartToPlan ";
            }
            return hql;
        }
        public IList<VCalendarTodoEO> LoadCalendarTodo(String rOwnerKey, String rUserKey, DateTime rStartToPlan1, DateTime rStartToPlan2,TodoStatus rStatus) {
            return  LoadCalendarTodo(rOwnerKey, rUserKey, rStartToPlan1.ToString("yyyy-MM-dd 00:00:00"), rStartToPlan2.ToString("yyyy-MM-dd 23:59:59"),rStatus);
        }
        public IList<VCalendarTodoEO> LoadCalendarTodo(String rOwnerKey, String rUserKey, String rStartToPlan1, String rStartToPlan2, TodoStatus rStatus) {
            String hql = BuildHQL(rOwnerKey, rUserKey,false, rStartToPlan1, rStartToPlan2, rStatus,null,null);
            return this.Find<VCalendarTodoEO>(hql);
        }

        public VCalendarTodoEO LoadCalendarTodo(String rOwnerKey, String rUserKey, String rTodoKey) {
            String hql = BuildHQL(rOwnerKey, rUserKey,true, null, null, TodoStatus.All, rTodoKey, null);
            IList<VCalendarTodoEO> list = this.Find<VCalendarTodoEO>(hql);
            if (list != null && list.Count > 0)
                return list[0];
            else
                return null;
        }


        public IList<Object> LoadCalendarTodo(string rOwnerKey, string rUserKey, DateTime rStartToPlan1, DateTime rStartToPlan2, TodoStatus rStatus, int rLimit) {
            return LoadCalendarTodo(rOwnerKey, rUserKey, rStartToPlan1.ToString("yyyy-MM-dd 00:00:00"), rStartToPlan2.ToString("yyyy-MM-dd 23:59:59"), rStatus, rLimit);
        }

        public IList<Object> LoadCalendarTodo(string rOwnerKey, string rUserKey, string rStartToPlan1, string rStartToPlan2, TodoStatus rStatus, int rLimit) {
            String hql = BuildHQL(rOwnerKey, rUserKey,false, rStartToPlan1, rStartToPlan2, rStatus, null, null);
            if (rLimit > 1) {
                return this.Find(hql,rLimit);
            } else {
                return this.Find(hql,10);
            }
        }
        public void CloseTodo(String rTodoKey, DateTime rDt) {
            TCalendarTodoEO eo = base.Load<TCalendarTodoEO>(rTodoKey);
            eo.EndToReal = DateTime.Now;
            if (eo.EndToPlan == null) {
                eo.EndToPlan = eo.EndToReal;
            }
            eo.IfClose = 1;
            base.Update<TCalendarTodoEO>(eo);
        }
    }
}
